package com.fita.admin.web.controller.finance;

import com.demo2do.core.entity.page.Page;
import com.demo2do.core.web.interceptor.MenuSetting;
import com.demo2do.core.web.utils.WebUtils;
import com.fita.admin.entity.search.order.OrderSearch;
import com.fita.admin.entity.security.Login;
import com.fita.admin.service.DataService;
import com.fita.admin.service.data.OrderSpecialEquipmentService;
import com.fita.admin.service.order.OrderService;
import com.fita.admin.service.report.ReportService;
import com.fita.admin.utils.FormatUtils;
import com.fita.admin.visitor.impl.PersistentVisitor;
import com.fita.admin.web.controller.view.Excel2TemplateView;
import com.fita.admin.web.resolver.Secure;
import com.fita.entity.data.Country;
import com.fita.entity.data.Supplier;
import com.fita.entity.order.Order;
import com.fita.entity.order.OrderSpecialEquipment;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 *
 * @author Ark
 *
 */
@Controller
@RequestMapping("/reports")
@MenuSetting("menu-report")
public class ReportController {
	
	private static final Logger logger = LoggerFactory.getLogger(ReportController.class);
	
	@Autowired
	private ReportService reportService;
	
	@Autowired
	private OrderService orderService;
	
	@Autowired
	@Qualifier("dataService")
	private DataService<Supplier> supplierService;

	@Autowired
	@Qualifier("dataService")
	private DataService<Country> countryService;
	
	@Autowired
	private PersistentVisitor persist;
	
	@Autowired
	private OrderSpecialEquipmentService orderSpecialEquipmentService;
	


    /**
     * list
     *
     * @param login
     * @return
     */
    @RequestMapping(value = "" , method = RequestMethod.GET)
    @MenuSetting("submenu-report-search")
    public ModelAndView list(@Secure Login login, Page page, HttpServletRequest request) {
                      
        ModelAndView modelAndView = new ModelAndView("report/report/report-list");
       
        int totals = reportService.count();
        
        if(totals > 0) {
        	
        	List<Order> orders = reportService.list(page.with(totals));
        	
        	modelAndView.addObject("orders", orders);
        	
        	modelAndView.addObject("page", page);
        	
        	modelAndView.addObject("queryString", WebUtils.extractQueryString(request, "page", "filter"));
        }
        
        return modelAndView;
        
    }
    
    /**
     * 打开查询面板
     * @return
     */
    @RequestMapping(value = "/search", method = RequestMethod.GET)
    @MenuSetting("submenu-report-search")
    public ModelAndView seach() {
    	
        ModelAndView modelAndView = new ModelAndView("report/report/report-search");
        
        List<Country> countries = countryService.listAll(Country.class);
       
        List<Supplier> suppliers = supplierService.listAll(Supplier.class);
       
        modelAndView.addObject("countries", countries);
       
        modelAndView.addObject("suppliers", suppliers);
        
        return modelAndView;
    }
    
    
    /**
     * 
     * @param search
     * @param request
     * @param page
     * @return
     */
    @RequestMapping(value = "", method = RequestMethod.GET, params = "filter")
    @MenuSetting("submenu-report-search")
    @ResponseBody
    public ModelAndView onSearch(OrderSearch search,HttpServletRequest request,Page page) {
       
    	ModelAndView modelAndView = new ModelAndView("report/report/report-list");
        //modelAndView.addObject("status", status);
    	// Status from search object
        
        int totals = orderService.count(search);
        
        if(totals > 0) {
            List<Order> orders = orderService.search(search, page.with(totals));
            modelAndView.addObject("orders", orders);
            modelAndView.addObject("page", page);
            modelAndView.addObject("queryString", WebUtils.extractQueryString(request, "page", "filter"));
        }
        
        return modelAndView;
    }
    
    /**
     * 导出所有记录
     * @param search 
     * @param page
     * @return
     */
    @RequestMapping(value = "/report-export", method = RequestMethod.GET)
    public ModelAndView reoprtExport(OrderSearch search, Page page,HttpServletRequest request) {
    	ModelAndView modelAndView = new ModelAndView(new Excel2TemplateView("report/financial-report.xlsx"));
    	 
		/*
		 * List<Map<String, Object>> results = reportService.search(search, page);
		 * modelAndView.addObject("results", results);
		 */
         
         
		/*
		 * Order order = orderService.load(Order.class, search.getOrderId());
		 * modelAndView.addObject("order", order);
		 */
    	 int totals = orderService.count(search);
    	 
    	  List<Order> results = orderService.search(search, page.with(totals));
    	 
    	  modelAndView.addObject("results", results);
    	  
    	  // calculate some datas.
    	  Map<String, Object> calculatedDatas = reportService.calculate(results);
    	  
    	  modelAndView.addObject("calculatedDatas", calculatedDatas);
		/*
		 * Map<String, Country> countryMap = (Map<String, Country>)
		 * countryService.listAll(Country.class); modelAndView.addObject("countryMap",
		 * countryMap);
		 */
         // 3. format util
         FormatUtils fmt = new FormatUtils();
         modelAndView.addObject("fmt", fmt);
        
         modelAndView.addObject("persist", persist);
         logger.warn("#reoprtExport() - export size=[{}]...", results.size());
         return modelAndView;
    	
    }
    
    
    /**
     * view
     * @param id
     * @return
     */
    @RequestMapping(value = "/{id}", method = RequestMethod.GET)
    @MenuSetting("submenu-report-search")
    public ModelAndView view(@PathVariable Long id) {
    	ModelAndView modelAndView = new ModelAndView("report/report/report-view");
        Order order = orderService.load(Order.class, id);
        List<OrderSpecialEquipment>  orderSpecialEquipment= orderSpecialEquipmentService.list(id);
		modelAndView.addObject("orderSpecialEquipment", orderSpecialEquipment);
        modelAndView.addObject("order", order);
      
        return modelAndView;
    }
    
}
